package com.sniff.atp.modules.test.process;

import com.ckbs.global.common.utils.DingTalkNoticeUtils;
import com.sniff.atp.modules.test.model.DemandReport;
import com.sniff.atp.modules.test.model.PublishPlan;
import com.sniff.atp.modules.test.service.PublishPlanService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: sniff-atp
 * @Package: com.sniff.atp.modules.test.process
 * @ClassName: BusinessPublishPlan
 * @Description: 通知产研中心今日提测需求
 * @Author: zss
 * @CreateDate: 2023/11/2 19:36
 * @UpdateDate: 2023/11/2 19:36
 * @Version: 1.0
 */

@Component
@Configuration
@EnableScheduling
public class TodaySubmitTestInfo {

    @Value("${sniff.autoInfoFlag}")
    private String autoInfoFlag;

    @Autowired
    private PublishPlanService publishPlanService;


//    @Scheduled(cron = "0/10 * * * * ?")
    @Scheduled(cron = "0 15 10 * * ?")
    private void configureTasks(){
        System.out.println("111111111111111");

        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 获取当天的0点时间的Date对象
        Date midnightDate = calendar.getTime();

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        PublishPlan publishPlan = new PublishPlan();
        publishPlan.setInfoTime(midnightDate);

        //通知技术
        List<DemandReport> publishPlanDeveList = publishPlanService.qryTodaySUbmitTestInfo(publishPlan);
        if(!publishPlanDeveList.isEmpty()){
            //通知内容
            String contentDev = "";
            contentDev = contentDev.concat("[快来][快来] 今日提测需求如下: \n\n");


            String addContentDev = "";
            //有值，通知客户
            for(int i=0;i<publishPlanDeveList.size();i++){
                addContentDev = addContentDev.concat(i + 1 + "、 【") ;
                addContentDev = addContentDev.concat(publishPlanDeveList.get(i).getDemandType()).concat("】");
                addContentDev = addContentDev.concat(publishPlanDeveList.get(i).getDemandName()).concat("----")
                ;


                boolean hasDevelopFlag = false;
//                获取产品
                String demandpd = publishPlanDeveList.get(i).getDemandPd();
                if(StringUtil.isNotBlank(demandpd) &&!"".equals(demandpd)){
                    hasDevelopFlag = true;
                    addContentDev = addContentDev.concat("产品：");
                    addContentDev = addContentDev.concat(demandpd).concat("、");
                }

                //获取后端开发
                String demandBackDevelop = publishPlanDeveList.get(i).getDemandBackDevelop();
                if(StringUtil.isNotBlank(demandBackDevelop) && !"".equals(demandBackDevelop)){
                    hasDevelopFlag = true;
                    addContentDev = addContentDev.concat("后端：");
                    addContentDev = addContentDev.concat(demandBackDevelop).concat("、");
                }
//                获取前端
                String demandFrontDevelop = publishPlanDeveList.get(i).getDemandFrontDevelop();
                if(StringUtil.isNotBlank(demandFrontDevelop) && !"".equals(demandFrontDevelop)){
                    hasDevelopFlag = true;
                    addContentDev = addContentDev.concat("前端：");
                    addContentDev = addContentDev.concat(demandFrontDevelop).concat("、");
                }
                //获取测试
                String demandTest = publishPlanDeveList.get(i).getDemandTest();
                if(StringUtil.isNotBlank(demandTest) && !"".equals(demandTest)){
                    hasDevelopFlag = true;
                    addContentDev = addContentDev.concat("测试：");
                    addContentDev = addContentDev.concat(demandTest).concat("、");
                }
                //获取数仓开发
                String dataDevelop = publishPlanDeveList.get(i).getDataDevelop();
                if(StringUtil.isNotBlank(dataDevelop) && !"".equals(dataDevelop)){
                    hasDevelopFlag = true;
                    addContentDev = addContentDev.concat("数据开发：");
                    addContentDev = addContentDev.concat(dataDevelop).concat("、");
                }

                //删除最后一个顿号
                if(hasDevelopFlag){
                    addContentDev = addContentDev.substring(0, addContentDev.length() - 1);
                }

                addContentDev = addContentDev.concat("\n");
            }
            //两个通知内容合并
            contentDev = contentDev.concat(addContentDev);
            contentDev = contentDev.concat("[加油]：请确认以上需求已正常提测，如有延期请及时联系对应测试[抱拳]  \n \n");

            // 发布钉钉通知测试群
            String robotToken = "1f02317c4339da2838e6efa6b3a7fe753ab9c72235c75c3b8d30a12c586ece14";
            String robotKey = "SEC29e67b7eb91cdd03f0a5e113843226d69a46dec847774ca3183a5d13614943e4";

            // 发布钉钉通知产研中心 -重要通知
            String robotToken2 = "04ba85ddf93abb1188c974bf386047bd232e0ac026609c30f02a1010f7a11d51";
            String robotKey2 = "SEC4dc0f576ee692982a7c390830b3839e536c1cb1e8d0c1186956f2e6daddddfea";

//
//            String robotToken = "c6c2c7c6c98c2fb5d234319afa9e0dad659152a8518ac739191811ce1bf8a67b";
//            String robotKey = "SECdf56e788259528d23a4b225863526a0bc426c178251313f1e4d5bdc5e9b696d6";



            //产研中心
            List<String> mobileList = new ArrayList<>();

            if("true".equals(autoInfoFlag)){
                try {
                    DingTalkNoticeUtils.sendDingMsgAll(robotKey,robotToken,mobileList,contentDev);
                    DingTalkNoticeUtils.sendDingMsgAll(robotKey2,robotToken2,mobileList,contentDev);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }
    }
}
